package com.sohoometer.utils;

import java.lang.reflect.Method;

import com.nari.heatshare.model.ITemperaturePointDataGather;

public class ParseUtils {

	/***
	 * 解析温度点数据 将二进制温度点序列解析为温度点对象
	 * */
	public static ITemperaturePointDataGather toTemperaturePointDataGather(StringBuilder parseTemp) {
		if(parseTemp==null||parseTemp.length()!=76) 
			return null;
		ITemperaturePointDataGather dataGather = new ITemperaturePointDataGather();
		Class<ITemperaturePointDataGather> tempClazz = ITemperaturePointDataGather.class;
		for(int i = 0;i<=23;i++) {
		//首先
			try {
				Double  temp = 	getHourTemp(parseTemp,i);
				//hour0Value
				//设置温度点的温度
				Method method = tempClazz.getMethod("setHour"+i+"Value", Double.class);
				method.invoke(dataGather, temp);
			} catch (Exception e) {
				continue;
			}
		}
		return dataGather;
	}

	private static Double getHourTemp(StringBuilder parseTemp, int i) {
		String integerPart = parseTemp.substring(2*i, 2*i+2);
		String floatPart = parseTemp.substring(i+24*2, i+24*2+1);
		String result = integerPart+"."+floatPart;
		return Double.valueOf(result);
	}

}
